package com.itheima.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageResult;
import com.itheima.service.ClazzService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;


@Service
public class ClazzServiceImpl extends ServiceImpl<ClazzMapper, Clazz> implements ClazzService {
    @Autowired
    private EmpService empService;


    @Override
    public PageResult page(String name, LocalDate begin, LocalDate end, int page, int pageSize) {
        LambdaQueryWrapper<Clazz> clazzWrapper = new LambdaQueryWrapper<>();
        clazzWrapper.like(name!=null&& name.isEmpty(),  Clazz::getName, name)
                .between(begin != null && end != null, Clazz::getBeginDate, begin, end);
        IPage<Clazz> clazzIPage = new Page<>(page, pageSize);
        IPage<Clazz> page1 = page(clazzIPage, clazzWrapper);

        List<Clazz> records = page1.getRecords();

        records.forEach(clazz -> {
            Emp byId = empService.getById(clazz.getMasterId());
            if (byId != null) {
                clazz.setMasterName(byId.getName());
            }
        });
        return new PageResult(page1.getTotal(), records);
    }
}
